#include<bits/stdc++.h>
using namespace std;
int x[1100];
int n;
int y[1100];
bool vis[1100];
queue<int>q;
int res = 0;
//第二题BFS遍历联通块
int main()
{
    while(cin >> n)
    {
        memset(vis,0,sizeof(vis));
        res = 0;
        for(int i = 0; i < n; i ++)
            cin >> x[i] >> y[i];
        for(int i = 0; i< n; i ++)
        {
            if(!vis[i])
            {
                q.push(i);
                while(!q.empty())
                {
                    int temp = q.front();
                    q.pop();
                    vis[temp] = 1;
                    for(int i = 0; i < n; i ++)
                    {
                        if(!vis[i] && ((x[i] == x[temp]) || (y[i]==y[temp])))
                        {
                            q.push(i);
                        }

                    }
                }
                res ++;

            }


        }
        cout << res - 1 << endl;

    }
    return 0;
}
